package com.whfc.fvs.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.fvs.dao.FvsSettingMapper;
import com.whfc.fvs.dto.FvsSettingDTO;
import com.whfc.fvs.entity.FvsSetting;
import com.whfc.fvs.enums.VsPlatform;
import com.whfc.fvs.service.FvsSettingService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : qzexing
 * @version : 1.0
 * @date : 2020-07-25 17:28
 */
@DubboService(interfaceClass = FvsSettingService.class, version = "1.0.0")
public class FvsSettingServiceImpl implements FvsSettingService {

    @Autowired
    private FvsSettingMapper fvsSettingMapper;


    @Override
    public List<FvsSettingDTO> getSrsSettingList() throws BizException {
        return fvsSettingMapper.selectSrsSettingList(VsPlatform.STREAM_SERVER.getValue());
    }

    @Override
    public List<FvsSettingDTO> getAppKeySecretList(VsPlatform platform) throws BizException {
        return fvsSettingMapper.selectAppKeySecretList(platform.getValue());
    }


    @Override
    public FvsSettingDTO getAppKeySecret(Integer deptId, VsPlatform platform) throws BizException {
        return fvsSettingMapper.selectAppKeySecret(deptId, platform.getValue());
    }

    @Override
    public List<FvsSettingDTO> getConfigList(Integer deptId) throws BizException {
        List<Integer> platforms = Arrays.asList(VsPlatform.HIK_EZVIZ.getValue(), VsPlatform.DAHUA_IMOU.getValue());
        return fvsSettingMapper.selectSettingList(deptId, platforms);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void platformConfigure(Integer deptId, List<FvsSettingDTO> list) throws BizException {
        //删除旧的数据
        fvsSettingMapper.logicDelByDeptId(deptId);
        //保存新配置
        List<FvsSetting> settings = new ArrayList<>();
        //萤石云配置数量
        int ezvizNum = 0;
        //乐橙云配置数量
        int imouNum = 0;
        for (FvsSettingDTO fvsSettingDTO : list) {
            Integer platform = fvsSettingDTO.getPlatform();
            if (VsPlatform.HIK_EZVIZ.getValue().equals(platform)) {
                ezvizNum += 1;
                if (ezvizNum > 1) {
                    throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "单个项目只允许一个萤石云配置");
                }
            } else if (VsPlatform.DAHUA_IMOU.getValue().equals(platform)) {
                imouNum += 1;
                if (imouNum > 1) {
                    throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "单个项目只允许一个乐橙云配置");
                }
            } else {
                //目前配置视频监控只支持海康萤石云或者大华乐橙云
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "平台类型错误");
            }
            FvsSetting fvsSetting = new FvsSetting();
            BeanUtils.copyProperties(fvsSettingDTO, fvsSetting);
            fvsSetting.setDeptId(deptId);
            settings.add(fvsSetting);
        }
        if (!settings.isEmpty()) {
            //保存新配置
            fvsSettingMapper.batchInsert(settings);
        }
    }
}
